import axios from 'axios'
import { userUserStore } from '@/store/user'
import NProgress from 'nprogress'
// 创建新的axios实例对象
// console.log(import.meta.env)
const service = axios.create({
  baseURL: import.meta.env.VITE_BASE_URL,
  timeout: 1000
})

// 添加请求拦截器
service.interceptors.request.use(
  (config) => {
    NProgress.start()
    //需要 token
    const { token } = userUserStore()
    //除过login,其他请求需要携带token
    if (config.url != '/auth/login') {
      config.headers.Authorization = 'Bearer ' + token
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
// 添加响应拦截器
service.interceptors.response.use(
  (response) => {
    NProgress.done()
    return response.data
  },
  (error) => {
    return Promise.reject(error)
  }
)

export default service
